﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Soopah.Xna.Input;
using System.IO;
using XNADirectInput;
using Microsoft.Xna.Framework;

namespace XDIbuttonMapper
{
    public partial class Form1 : Form
    {

        public DirectInputGamepad gp;
        public ButtonSettings bSettings;
        public XDIMapper game;
        

        public IntPtr getDrawSurface()
        {
            return pbXNA.Handle;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Reload();
        }

        private void cbGamePads_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbGamePads.SelectedIndex >= 0 && DirectInputGamepadManager.Gamepads[cbGamePads.SelectedIndex] != null)
            {
                gp = DirectInputGamepadManager.Gamepads[cbGamePads.SelectedIndex];
                if (game != null)
                    game.gp = gp;
                for (int i = 0; i < gp.Device.Caps.NumberButtons; i++)
                {
                    cbA.Items.Add(i + 1);
                    cbB.Items.Add(i + 1);
                    cbX.Items.Add(i + 1);
                    cbY.Items.Add(i + 1);
                    cbLB.Items.Add(i + 1);
                    cbRB.Items.Add(i + 1);
                    cbBack.Items.Add(i + 1);
                    cbStart.Items.Add(i + 1);
                    cbLS.Items.Add(i + 1);
                    cbRS.Items.Add(i + 1);
                }
                if (game != null && game.xdi != null)
                {
                    if (game.xdi.bSettings.TryGetValue(gp.Device.DeviceInformation.ProductName.Trim(), out bSettings))
                    {
                        LoadSettings();
                    }
                }
                if (File.Exists(@"Devices\" + gp.Device.DeviceInformation.ProductName.Trim()))
                {
                    LoadSettings(@"Devices\" + gp.Device.DeviceInformation.ProductName.Trim());
                }

            }
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            Reload();
        }

        private void Reload()
        {
            cbGamePads.Items.Clear();
            DirectInputGamepadManager.ReloadGamepads();
            for (int i = 0; i < DirectInputGamepadManager.Gamepads.Count; i++)
            {
                cbGamePads.Items.Add(DirectInputGamepadManager.Gamepads[i].PadName.Trim());
            }
            if (cbGamePads.Items.Count > 0)
            {
                cbGamePads.SelectedIndex = 0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ofdOpen.FileName = gp.Device.DeviceInformation.ProductName;
            if (ofdOpen.ShowDialog() == DialogResult.OK)
            {
                LoadSettings(ofdOpen.FileName);
            }
        }

        private void LoadSettings(string path)
        {
            try
            {
                gp.LoadSettings(path);
                bSettings = gp.bSettings;
                LoadSettings();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadSettings()
        {
            ResetControls();
            game.gp.bSettings = bSettings;

            cbA.SelectedIndex = bSettings.AIndex;
            cbB.SelectedIndex = bSettings.BIndex;
            cbX.SelectedIndex = bSettings.XIndex;
            cbY.SelectedIndex = bSettings.YIndex;

            cbLB.SelectedIndex = bSettings.LBIndex;
            cbRB.SelectedIndex = bSettings.RBIndex;

            cbBack.SelectedIndex = bSettings.BackIndex;
            cbStart.SelectedIndex = bSettings.StartIndex;

            cbLS.SelectedIndex = bSettings.LStickIndex;
            cbRS.SelectedIndex = bSettings.RStickIndex;

            ckThird.Checked = bSettings.HasShoulders;
            ckRight.Checked = bSettings.HasRightStick;
            ckLeft.Checked = bSettings.HasLeftStick;

            foreach (AnalogInput AI in bSettings.Sticks)
            {
                switch (AI.Index)
                {
                    case 1:
                        cbXAxisLS.Text = AI.XAxis;
                        cbYAxisLS.Text = AI.YAxis;
                        break;
                    case 2:
                        cbYAxisRS.Text = AI.YAxis;
                        cbXAxisRS.Text = AI.XAxis;
                        break;
                    case 3:
                        cbLeftShoulder.Text = AI.XAxis;
                        cbRightShoulder.Text = AI.YAxis;
                        break;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ApplySettings();
                sfdSave.FileName = gp.Device.DeviceInformation.ProductName;
                if (sfdSave.ShowDialog() == DialogResult.OK)
                {
                    gp.SaveSettings(sfdSave.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ApplySettings()
        {
            if (bSettings == null)
                bSettings = new ButtonSettings();
            bSettings.Name = cbGamePads.Text;
            bSettings.AIndex = cbA.SelectedIndex;
            bSettings.BIndex = cbB.SelectedIndex;
            bSettings.XIndex = cbX.SelectedIndex;
            bSettings.YIndex = cbY.SelectedIndex;
            bSettings.BackIndex = cbBack.SelectedIndex;
            bSettings.StartIndex = cbStart.SelectedIndex;
            bSettings.LBIndex = cbLB.SelectedIndex;
            bSettings.LStickIndex = cbLS.SelectedIndex;
            bSettings.RBIndex = cbRB.SelectedIndex;
            bSettings.RStickIndex = cbRS.SelectedIndex;

            bSettings.Sticks.Clear();

            bSettings.HasLeftStick = ckLeft.Checked;
            bSettings.HasRightStick = ckRight.Checked;
            bSettings.HasShoulders = ckThird.Checked;

            if (bSettings.HasLeftStick)
            {
                AnalogInput Ai = new AnalogInput();
                Ai.Index = 1;
                Ai.XAxis = cbXAxisLS.Text;
                Ai.YAxis = cbYAxisLS.Text;
                bSettings.Sticks.Add(Ai);
            }

            if (bSettings.HasRightStick)
            {
                AnalogInput Ai = new AnalogInput();
                Ai.Index = 2;
                Ai.XAxis = cbXAxisRS.Text;
                Ai.YAxis = cbYAxisRS.Text;
                bSettings.Sticks.Add(Ai);
            }
            if (bSettings.HasShoulders)
            {
                AnalogInput Ai = new AnalogInput();
                Ai.Index = 3;
                Ai.XAxis = cbLeftShoulder.Text;
                Ai.YAxis = cbRightShoulder.Text;
                bSettings.Sticks.Add(Ai);
            }

            if (gp != null)
            {
                gp.bSettings = bSettings;
                game.gp.bSettings = bSettings;
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            game.Exit();
        }

        private void ResetControls()
        {
            ckRight.Checked = true;
            ckLeft.Checked = true;
            ckThird.Checked = true;
            cbYAxisLS.SelectedIndex = -1;
            cbYAxisRS.SelectedIndex = -1;
            cbXAxisLS.SelectedIndex = -1;
            cbXAxisRS.SelectedIndex = -1;
            cbRightShoulder.SelectedIndex = -1;
            cbLeftShoulder.SelectedIndex = -1;

            cbY.SelectedIndex = -1;
            cbB.SelectedIndex = -1;
            cbA.SelectedIndex = -1;
            cbX.SelectedIndex = -1;
            cbStart.SelectedIndex = -1;
            cbBack.SelectedIndex = -1;
            cbLB.SelectedIndex = -1;
            cbRB.SelectedIndex = -1;
            cbRS.SelectedIndex = -1;
            cbLS.SelectedIndex = -1;


        }

        private void ckLeft_CheckedChanged(object sender, EventArgs e)
        {
            gpLS.Enabled = ckLeft.Checked;
        }

        private void ckRight_CheckedChanged(object sender, EventArgs e)
        {
            gpRS.Enabled = ckRight.Checked;
        }

        private void ckThird_CheckedChanged(object sender, EventArgs e)
        {
            gpShoulders.Enabled = ckThird.Checked;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (game != null)
            {
                game.ShowDiagnostic = !game.ShowDiagnostic;
                if (game.ShowDiagnostic)
                    btnToggle.Text = "Toggle Display Control View";
                else
                    btnToggle.Text = "Toggle Display Diags View";
            }
        }

        public void GameActivated(object sender, EventArgs e)
        {
            cbGamePads_SelectedIndexChanged(sender, e);
        }
    }
}
